/**
 * 创建动画
 */
function addAimation() {
  // js创建@keyframes动画：旋转360度
  var htmlFrame = `
  #g-div-loading {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    height: 120px;
    width: 120px;
  }
  .g-div-demo {
    width: 10px;
    height: 10px;
    border-radius: 5px;
    background: rgb(162, 167, 118);
    position: absolute;
    animation: g-div-demo linear 1.1s infinite;
  }
  .g-div-demo:nth-child(1) {
    top: 2px;
    left: 55px;
    animation-delay: 0s;
  }
  .g-div-demo:nth-child(2) {
    top: 10px;
    left: 80px;
    animation-delay: 0.1s;
  }
  .g-div-demo:nth-child(3) {
    top: 30px;
    left: 100px;
    animation-delay: 0.2s;
  }
  .g-div-demo:nth-child(4) {
    top: 55px;
    left: 108px;
    animation-delay: 0.3s;
  }
  .g-div-demo:nth-child(5) {
    top: 80px;
    left: 100px;
    animation-delay: 0.4s;
  }
  .g-div-demo:nth-child(6) {
    top: 100px;
    left: 80px;
    animation-delay: 0.5s;
  }
  .g-div-demo:nth-child(7) {
    top: 108px;
    left: 55px;
    animation-delay: 0.6s;
  }
  .g-div-demo:nth-child(8) {
    top: 100px;
    left: 30px;
    animation-delay: 0.7s;
  }
  .g-div-demo:nth-child(9) {
    top: 80px;
    left: 10px;
    animation-delay: 0.8s;
  }
  .g-div-demo:nth-child(10) {
    top: 55px;
    left: 2px;
    animation-delay: 0.9s;
  }
  .g-div-demo:nth-child(11) {
    top: 30px;
    left: 10px;
    animation-delay: 1s;
  }
  .g-div-demo:nth-child(12) {
    top: 10px;
    left: 30px;
    animation-delay: 1.1s;
  }
  @keyframes g-div-demo {
    0% {
      transform: scale(1);
      background: rgb(162, 167, 118);
    }
    25% {
      transform: scale(2);
      background: rgba(162, 167, 118, 0.8);
    }
    50% {
      transform: scale(1);
      background: rgb(162, 167, 118, 0.6);
    }
    75% {
      transform: scale(1);
      background: rgb(162, 167, 118, 0.4);
    }
    100% {
      transform: scale(1);
      background: rgb(162, 167, 118, 0.2);
    }
  }`;
  // 创建style标签
  var style = document.createElement('style');
  // 将 keyframes样式写入style内
  style.innerHTML = htmlFrame;
  // 将style样式存放到head标签
  document.getElementsByTagName('head')[0].appendChild(style);
  return style;
}

/**
 * 向Html中添加loading元素
 */
function addLoading() {
  //添加动画
  var style = addAimation();

  //在页面未加载完毕之前显示的loading Html自定义内容
  var htmlLoading = `
  <div id="g-div-loading">
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
    <div class="g-div-demo"></div>
  </div>`;

  //呈现loading效果
  document.write(htmlLoading);

  //监听加载状态改变
  document.onreadystatechange = function () {
    //加载状态为complete时移除loading效果
    if (document.readyState == 'complete') {
      var view = document.getElementById('g-div-loading');
      view.parentNode.removeChild(view);
      style.parentNode.removeChild(style);
    }
  };
}

addLoading();
